﻿<template>
    <el-dialog title="打印选择" :close-on-click-modal="false" :visible.sync="visible" :append-to-body="true"
        class="SHZY-dialog SHZY-dialog_center" lock-scroll width="50%">
        <el-row :gutter="15" class="" :style="{ margin: '0 auto', width: '100%' }">
            <el-form ref="elForm" size="small" label-width="100px" label-position="left" :model="dataForm"
                :rules="rules">
                <el-col :span="24">
                    <el-form-item label-width="200" label="打印类型：" prop="type">
                        <el-checkbox-group v-model="dataForm.checkList">
                            <el-checkbox label="现场调查"></el-checkbox>
                            <el-checkbox label="纠正和预防"></el-checkbox>
                            <el-checkbox label="质量申诉"></el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>
                </el-col>
            </el-form>
        </el-row>

        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">{{ $t('common.cancelButton') }}</el-button>
            <el-button :loading="btnLoading" type="primary" @click="dataFormSubmit()">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>

import customerAPI from '@/api/q1/business/customer';
export default {
    data() {
        return {
            btnLoading: false,
            visible: false,
            dataForm:
            {
                type: undefined,
                CustomerId: undefined,
                checkList: [],
            },
            rules: {
                projectName: [{ required: true, message: '请填写项目名称', trigger: 'blur' },],
            },

            printConfigId: "573376979706308997",
        }
    },
    created() {

    },
    methods: {

        init(id) {

            this.visible = true
            this.$nextTick(() => {

                this.$refs['elForm'].resetFields();
                this.dataForm.CustomerId = id;
                this.dataForm.checkList = [];
            })
        },

        dataFormSubmit() {
            if (this.dataForm.checkList.length > 0) {

                this.btnLoading = true
                console.log("🚀 ~ this.dataForm.checkList:", this.dataForm.checkList)
                if (this.dataForm.checkList.includes("现场调查")) {
                    this.printConfigId = '575212521523774661'
                    let query = {
                        exportType: 1,
                        list: {
                            printConfigId: this.printConfigId,
                            formDataId: this.dataForm.CustomerId,
                        },
                    };
                    customerAPI.PrintCustomerSurvey(query).then((res) => {
                        console.log(res)
                        this.btnLoading = false;
                        if (res.byteLength > 117) {
                            const blob = new Blob([res], { type: 'application/pdf' })
                            const url = window.URL.createObjectURL(blob)
                            window.open(`${url}#filename=Zy`, "_blank")
                            this.visible = false
                        } else {
                            this.$message({
                                message: "打印出错!",
                                type: "error",
                                duration: 1000
                            });
                        }
                    }).catch((res) => {
                        this.btnLoading = false;
                    })

                }
                if (this.dataForm.checkList.includes("纠正和预防")) {
                    this.printConfigId = '575213750031221958'
                    let query = {
                        exportType: 1,
                        list: {
                            printConfigId: this.printConfigId,
                            formDataId: this.dataForm.CustomerId,
                        },
                    };
                    customerAPI.PrintCustomerMeasure(query).then((res) => {
                        console.log(res)
                        this.btnLoading = false;
                        if (res.byteLength > 117) {
                            const blob = new Blob([res], { type: 'application/pdf' })
                            const url = window.URL.createObjectURL(blob)
                            window.open(`${url}#filename=Zy`, "_blank")
                            this.visible = false
                        } else {
                            this.$message({
                                message: "打印出错!",
                                type: "error",
                                duration: 1000
                            });
                        }
                    }).catch((res) => {
                        this.btnLoading = false;
                    })

                }
                if (this.dataForm.checkList.includes("质量申诉")) {
                    this.printConfigId = '573376979706308997'
                    let query = {
                        exportType: 1,
                        list: {
                            printConfigId: this.printConfigId,
                            formDataId: this.dataForm.CustomerId,
                        },
                    };
                    customerAPI.PrintCustpmerAppeal(query).then((res) => {
                        console.log(res)
                        this.btnLoading = false;
                        if (res.byteLength > 117) {
                            const blob = new Blob([res], { type: 'application/pdf' })
                            const url = window.URL.createObjectURL(blob)
                            window.open(`${url}#filename=Zy`, "_blank")
                            this.visible = false
                        } else {
                            this.$message({
                                message: "打印出错!",
                                type: "error",
                                duration: 1000
                            });
                        }
                    }).catch((res) => {
                        this.btnLoading = false;
                    })

                }

            } else {
                this.$message({
                    message: "请选择要打印的类型",
                    type: "error",
                    duration: 1000
                });
            }




        }
    }
}
</script>